package cn.iocoder.yudao.module.project.service.kismaterialreceipt;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kismaterialreceipt.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterialreceipt.KisMaterialReceiptDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kismaterialreceipt.KisMaterialReceiptMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 物料入库单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KisMaterialReceiptServiceImpl implements KisMaterialReceiptService {

    @Resource
    private KisMaterialReceiptMapper kisMaterialReceiptMapper;

    @Override
    public String createKisMaterialReceipt(KisMaterialReceiptSaveReqVO createReqVO) {
        // 插入
        KisMaterialReceiptDO kisMaterialReceipt = BeanUtils.toBean(createReqVO, KisMaterialReceiptDO.class);
        kisMaterialReceiptMapper.insert(kisMaterialReceipt);
        // 返回
        return kisMaterialReceipt.getId();
    }

    @Override
    public void updateKisMaterialReceipt(KisMaterialReceiptSaveReqVO updateReqVO) {
        // 校验存在
        validateKisMaterialReceiptExists(updateReqVO.getId());
        // 更新
        KisMaterialReceiptDO updateObj = BeanUtils.toBean(updateReqVO, KisMaterialReceiptDO.class);
        kisMaterialReceiptMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisMaterialReceipt(String id) {
        // 校验存在
        validateKisMaterialReceiptExists(id);
        // 删除
        kisMaterialReceiptMapper.deleteById(id);
    }

    private void validateKisMaterialReceiptExists(String id) {
        if (kisMaterialReceiptMapper.selectById(id) == null) {
            throw exception(KIS_MATERIAL_RECEIPT_NOT_EXISTS);
        }
    }

    @Override
    public KisMaterialReceiptDO getKisMaterialReceipt(String id) {
        return kisMaterialReceiptMapper.selectById(id);
    }

    @Override
    public PageResult<KisMaterialReceiptDO> getKisMaterialReceiptPage(KisMaterialReceiptPageReqVO pageReqVO) {
        return kisMaterialReceiptMapper.selectPage(pageReqVO);
    }




    @Override
    public void insertRecord(List<KisMaterialReceiptDO> insertList) {
        // 插入
        if (null != insertList && insertList.size() > 0) {
            kisMaterialReceiptMapper.insertList(insertList);
        }
    }

    @Override
    public List<MaterialReceiptReportVO> selectMaterialReceiptReportByDate(MaterialReceiptReportBO materialReceiptReportBO) {
        List<MaterialReceiptReportVO> materialReceiptReportVOS = kisMaterialReceiptMapper.selectMaterialReceiptReportByDate(materialReceiptReportBO);
        return materialReceiptReportVOS;
    }

    @Override
    public Long countMaterialReceiptList(Map<String, Object> params) {
        Long count = kisMaterialReceiptMapper.countMaterialReceiptList(params);
        return count;
    }

    @Override
    public List<KisMaterialReceiptListVO> selectMaterialReceiptList(Map<String, Object> params) {
        List<KisMaterialReceiptListVO> kisMaterialReceiptListVOS = kisMaterialReceiptMapper.selectMaterialReceiptList(params);
        return kisMaterialReceiptListVOS;
    }

}